/**

  Copyright xuehuiit Corp. 2018 All Rights Reserved.

  http://www.xuehuiit.com

  QQ 411321681

*/

package main

import (


	"fmt"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
	/*"github.com/hyperledger/fabric/common/attrmgr"
	 */
	//"github.com/hyperledger/fabric/common/util"
	//"encoding/json"
	//"strings"
	//"time"
	/*"bytes"
	"time"
	"encoding/gob"
	"github.com/hyperledger/fabric/common/tools/protolator"
	"github.com/hyperledger/fabric/core/chaincode/shim/ext/statebased"
	"encoding/pem"
	"crypto/x509"*/
)


//定义一个机构体，作为chaincode的主对象，可以是任何符合go语言规范的命名方式
type fabriccc struct {

}


/**



    peer chaincode install -n fb14_cc_shbc_1221 -v 1.0 -p gitee.com/robertrfeng/fabric_cc_study/robertfeng



	系统初始化方法， 在部署chaincode的过程中当执行命令

    peer chaincode instantiate -o orderer.qklszzn.com:7050 -C mytestchannel -n fb14_cc_shbc_1221 -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"


    的时候会调用该方法


	https://github.com/hyperledger/fabric/blob/release/core/chaincode/shim/interfaces.go  所有的注释这里

*/
func (t *fabriccc) Init(stub shim.ChaincodeStubInterface) pb.Response {

	fmt.Println(" <<  ========  success init it is view in docker  ==========  >> ")
	return shim.Success([]byte("success init "))
}

/**

  主业务逻辑，执行命令
  peer chaincode invoke -o 192.168.23.212:7050 -C roberttestchannel -n r_test_cc6 -c '{"Args":["invoke","a","b","1"]}'

  的时候系统会调用该方法并传入相关的参数，注意 "invoke" 之后的参数是需要传入的参数



*/

func (t *fabriccc) Invoke(stub shim.ChaincodeStubInterface) pb.Response {


	function,args := stub.GetFunctionAndParameters()

	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]


	fmt.Println("  ========  curr method is big invoke   ========== ")
	fmt.Printf(" parm is  %s  %s  %s  %s  \n " , a_parm , b_parm , c_parm ,function)


	if function == "get" {
		return t.get(stub, args)
	}else if function == "set" {
		return t.set(stub, args)
	}else if function == "delete" {
		return t.delete(stub, args)
	}

	/*else if function == "GetHistoryForKey" {
		return t.GetHistoryForKey(stub, args)
	}else if function == "addNewBc" {
		return t.addNewBc(stub,args)
	}else if function == "querySearch" {
		return t.querySearch(stub,args)
	}else if function == "querySearch4Composite" {
		return t.querySearch4Composite(stub,args)
	}else if function == "set4Composite" {
		return t.set4Composite(stub,args)
	}else if function == "rangeQuery"{
		return t.rangeQuery(stub,args)
	}else if function == "querySearch4Pageing"{
		return t.querySearch4Pageing(stub,args)
	}else if function == "querySearch4Composite4Pageing"{
		return t.querySearch4Composite4Pageing(stub,args)
	}else if function == "rangeQuery4Pageing"{

		return t.rangeQuery4Pageing(stub,args)

	}else if function == "setPrivate" {

		return t.setPrivate(stub,args)

	}else if function == "getPrivate" {

		return t.getPrivate(stub,args)

	}else if function == "getChannelID" {

		return t.getChannelID(stub,args)

	}else if function == "getBinding" {

		return t.getBinding(stub,args)

	}else if function == "getTransient" {

		return t.getTransient(stub)

	}else if function == "getCreator" {

		return t.getCreator(stub)

	}else if function == "getSignedProposal" {

		return t.getSignedProposal(stub)

	}else if function == "getTxTimeStamp" {

		return t.getTxTimeStamp( stub )

	}else if function == "setStateValidationParameter" {

		return t.setStateValidationParameter( stub ,args)

	}else if function == "getStateValidationParameter" {

		return t.getStateValidationParameter( stub ,args )

	}else if function == "InvokeChaincode"{

		return t.InvokeChaincode( stub ,args )

	}else if function == "GetTxID" {
		return t.getTxID(stub)

	}*/


	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")


}



// test get method
/**

调用方法

peer chaincode invoke -o orderer.qklszzn.com:7050  --waitForEvent -C mytestchannel -n fb14_cc_shbc_1221  -c '{"Args":["set","get","key1222","real cc MSP1 2019-12-22 10:22:36"]}'


调用方法

peer chaincode query -C mytestchannel -n fb14_cc_shbc_1221 -c '{"Args":["get","get","key1222","chaincode v 1.5 = query11111"]}'

*/
func (t *fabriccc) get(stub shim.ChaincodeStubInterface, args []string) pb.Response {

	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]

	fmt.Println("  ========  curr method is get   ========== ")
	fmt.Printf(" parm is  %s  %s  %s   \n " , a_parm , b_parm , c_parm )

	keyvalue,err := stub.GetState(b_parm)

	if( err != nil  ){

		return shim.Error(" finad error! ")
	}

	return shim.Success( keyvalue )

}


// test delete method
func (t *fabriccc) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {


	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]

	fmt.Println("  ========  curr method is delete   ========== ")
	fmt.Printf(" parm is  %s  %s  %s   \n " , a_parm , b_parm , c_parm )


	err := stub.DelState(b_parm)

	if err != nil {
		return shim.Error(" 删除出现错误！！！！！")
	}

	return shim.Success([]byte(" 删除正确！！！！！  "))


}



// test set method
func (t *fabriccc) set(stub shim.ChaincodeStubInterface, args []string) pb.Response {


	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]


	fmt.Println("  ========  curr method is set   ========== ")

	fmt.Printf(" parm is  %s  %s  %s  \n " , a_parm , b_parm , c_parm )

	stub.PutState(b_parm,[]byte(c_parm))
	return shim.Success( []byte( "success invok " + c_parm  )  )


}




func main() {
	err := shim.Start(new(fabriccc))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}

